package cn.lyvust.first.manage.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import cn.lyvust.first.common.util.TreeUtil;
import cn.lyvust.first.manage.dto.param.evaluation.ProjectItemParam;
import cn.lyvust.first.manage.dto.param.evaluation.ProjectItemRecordParam;
import cn.lyvust.first.manage.dto.result.ProjectItemRecordStatResp;
import cn.lyvust.first.manage.entity.EvaluationProjectItem;
import cn.lyvust.first.manage.mapper.EvaluationProjectItemMapper;
import cn.lyvust.first.manage.service.EvaluationProjectItemRecordService;
import cn.lyvust.first.manage.service.EvaluationProjectItemService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ggzheng
 * @since 2021-01-19
 */
@Service
public class EvaluationProjectItemServiceImpl extends ServiceImpl<EvaluationProjectItemMapper, EvaluationProjectItem> implements EvaluationProjectItemService {
    @Autowired
    private EvaluationProjectItemRecordService projectItemRecordService;

    @Override
    public JSONArray listAll(ProjectItemParam param) {
        QueryWrapper wrapper = new QueryWrapper();
        if (!StringUtils.isEmpty(param.getId())) {
            wrapper.eq("id", param.getId());
        }
        if (!StringUtils.isEmpty(param.getPid())) {
            wrapper.eq("pid", param.getPid());
        }
        if (!StringUtils.isEmpty(param.getTitle())) {
            wrapper.eq("title", param.getTitle());
        }
        if (!StringUtils.isEmpty(param.getStatus())) {
            wrapper.eq("status", param.getStatus());
        }
        if (!StringUtils.isEmpty(param.getScore())) {
            wrapper.eq("score", param.getScore());
        }
        if (!StringUtils.isEmpty(param.getProjectId())) {
            wrapper.eq("project_id", param.getProjectId());
        }
        if (!StringUtils.isEmpty(param.getIsNeedScore())) {
            wrapper.eq("is_need_score", param.getIsNeedScore());
        }
        wrapper.orderByAsc("order_id");
        Integer total = 0;
        List<EvaluationProjectItem> resList = this.list(wrapper);
        if (!StringUtils.isEmpty(param.getUserId())) {
            for (EvaluationProjectItem item : resList) {
                total = this.statTotalRecord(param.getUserId(), item.getId());
                item.setTotal(total);
            }
        }
        JSONArray result = TreeUtil.listToTree(JSONArray.parseArray(JSON.toJSONString(resList)),"id","pid","children");
        return result;
    }

    private Integer statTotalRecord(String userId, String itemId) {
        Integer total = 0;
        try {
            ProjectItemRecordParam param = new ProjectItemRecordParam();
            param.setUserId(userId);
            param.setItemId(itemId);
            ProjectItemRecordStatResp res = projectItemRecordService.stat(param);
            total = res.getTotal();
        } catch (Exception exception) {
            // 吃掉异常
        }
        return total;
    }
}
